package com.sqly.doc.user;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.adapter.CommonAdapter;
import com.sqly.doc.adapter.ViewHolder;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.pojo.Doctor;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.XListView;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * 选择用户界面
 * Created by Administrator on 2017-11-14.
 */

public class choose_doc extends MyBaseActivity {


    List<Doctor> list;//信息列表
    XListView listView;
    ZLoadingDialog dialog;//加载动画
    Context con;
    private int pageNum1 = 1;// 无关键字页数
    EditText key;
    TextView title, uname;
    LinearLayout back;

    @Override
    protected void initUI() {
        setContentView(R.layout.choose_doc);
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("选择医生");
        uname.setVisibility(View.INVISIBLE);
        con = choose_doc.this;
        back = (LinearLayout)findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        key = (EditText) findViewById(R.id.key);
        key.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                getJumin();
            }
        });
        listView = (XListView) findViewById(R.id.listView);
        listView.setPullLoadEnable(false);
        dialog = new ZLoadingDialog(con);
        dialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
                .setLoadingColor(Color.parseColor("#1C86EE"))//颜色
                .setHintText("Loading...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.parseColor("#1C86EE"));  // 设置字体颜色

    }

    @Override
    protected void initData() {

        getJumin();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(con, getIntent().getStringExtra("cname").getClass());
                Bundle b = new Bundle();
                b.putSerializable("j", list.get(i - 1));
                intent.putExtras(b);
                setResult(0, intent);
                finish();
            }
        });
       /* getDataList();*/
    }

    @Override
    protected void initListener() {

    }

    //更新居民信息
    //网络请求居民信息
    public void getJumin() {
        RequestParams ps = new RequestParams();
        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
        ps.add("type", "2");
        if (key.getText().toString().length() != 0)
            ps.add("XINGMING", key.getText().toString());

        UserClient.get(HttpUrl.GetDoctor, ps, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                MyToastUtil.ShowToast(con, "请求失败");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                String content = new String(response);
                dialog.dismiss();
                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                if (ret.getCode().equals("200")) {
                    List<Doctor> ll = JSON.parseArray(
                            ret.getRd(), Doctor.class);
                    if (pageNum1 == 1)
                        list = new ArrayList<Doctor>();
                    for (int i = 0; i < ll.size(); i++) {
                        list.add(ll.get(i));
                    }
                    listView.setSelection(list.size() - ll.size());
                    listView.stopRefresh();
                    listView.stopLoadMore();
                    listView.setRefreshTime("刚刚");
                    listView.setXListViewListener(new XListView.IXListViewListener() {
                        @Override
                        public void onRefresh() {
                            getJumin();
                        }

                        @Override
                        public void onLoadMore() {
                        }
                    });
                    listView.setAdapter(new CommonAdapter<Doctor>(con, list, R.layout.jumin_item) {
                        @Override
                        public void convert(ViewHolder helper, Doctor item) {
                            helper.setText(R.id.cardnum, item.getCode());
                            helper.setText(R.id.name, item.getName());
                            TextView number = (TextView)helper.getConvertView().findViewById(R.id.number);
                            number.setVisibility(View.GONE);
                        }
                    });

                } else {
                    MyToastUtil.ShowToast(con, "获取错误");
                }

            }
        });


    }
}
